import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {HttpClient} from '@angular/common/http';
import {HTTP_URLS} from '../../config/app-http.url';
import {map} from 'rxjs/operators';
import {CommonTool} from '../common.tool';

@Injectable({
  providedIn: 'root'
})
export class DictService {

  private dictKey = 'dictKey';

  constructor(private http: HttpClient) {
  }

  /**
   * 给formInfo设置回填信息
   */
  public setFormInfo(formInfo: any, field: string, dictType?: string) {
    const dictList = this.getCacheDictList(dictType);
    if (!dictList || dictList.length === 0) {
      const url = CommonTool.analysisParam(HTTP_URLS.getMyAppDictList, {type: dictType});
      this.http.get<Array<any>>(url).pipe(map(res => {
        const json = localStorage.getItem(this.dictKey);
        let dictMap = {};
        if (json) {
          dictMap = JSON.parse(json);
        }
        dictMap[dictType] = res;
        localStorage.setItem(this.dictKey, JSON.stringify(dictMap));
        if (formInfo[field]) {
          for (const data of dictList) {
            if (formInfo[field] + '' === data['value']) {
              formInfo[field] = data;
            }
          }
        }
      }));
    } else {
      if (formInfo[field]) {
        for (const data of dictList) {
          if (formInfo[field] + '' === data['value']) {
            formInfo[field] = data;
          }
        }
      }
    }
  }

  /**
   * 根据字典类型获取字典集合
   */
  public getCacheAndDictListByType(dictType?: string): Observable<any> {
    if (dictType) {
      const dictList = this.getCacheDictList(dictType);
      if (!dictList || dictList.length === 0) {
        const url = CommonTool.analysisParam(HTTP_URLS.getMyAppDictList, {type: dictType});
        return this.http.get<Array<any>>(url).pipe(map(res => {
          const json = localStorage.getItem(this.dictKey);
          let dictMap = {};
          if (json) {
            dictMap = JSON.parse(json);
          }
          dictMap[dictType] = res;
          localStorage.setItem(this.dictKey, JSON.stringify(dictMap));
          return res;
        }));
      }
      return new Observable((observer) => {
        observer.next(dictList);
      });
    }
  }

  /**
   * 获取缓存和字典类型获取字典名称
   */
  public getCacheAndDictDescByType(value: string, dictType?: string): Observable<string> {
    const dictList = this.getCacheDictList(dictType);
    if (!dictList || dictList.length === 0) {
      const url = CommonTool.analysisParam(HTTP_URLS.getMyAppDictList, {type: dictType});
      return this.http.get<Array<any>>(url).pipe(map(res => {
        const json = localStorage.getItem(this.dictKey);
        let dictMap = {};
        if (json) {
          dictMap = JSON.parse(json);
        }
        dictMap[dictType] = res;
        localStorage.setItem(this.dictKey, JSON.stringify(dictMap));
        for (const arr of res) {
          if (arr.value === (value + '')) {
            return arr.name;
          }
        }
      }));
    }
    if (dictList && dictList.length > 0) {
      for (const arr of dictList) {
        if (arr.value === (value + '')) {
          return new Observable((observer) => {
            observer.next(arr.name);
          });
        }
      }
    }
  }

  /**
   * 获取全部字典
   */
  getDictMap() {
    return this.http.get<Array<any>>(HTTP_URLS.getMyAppDictList).pipe(map(res => {
      const dictMap = {};
      for (const row of res) {
        if (dictMap[row.type] && dictMap[row.type].length > 0) {
          dictMap[row.type].push({
            name: row['name'],
            value: row['value']
          });
        } else {
          dictMap[row.type] = [{
            name: row['name'],
            value: row['value']
          }];
        }
      }
      localStorage.setItem(this.dictKey, JSON.stringify(dictMap));
      return dictMap;
    }));
  }

  /**
   * 获取缓存字典集合
   */
  public getCacheDictList(type) {
    const json = localStorage.getItem(this.dictKey);
    let dictMap = {};
    if (json) {
      dictMap = JSON.parse(json);
    }
    return dictMap[type];
  }
}
